/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dal;

import entity.Field_Event;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Hailk_c00639
 */
public class Field_EventDAL {

    MyConnection con = new MyConnection();

    public List<Field_Event> getAllFieldEvent() {
        List<Field_Event> lst = new ArrayList<>();

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetAllFieldEvent}");
            ResultSet rs = call.executeQuery();

            while (rs.next()) {
                Field_Event fieldEvent = new Field_Event();
                fieldEvent.setFieldID(rs.getInt("FieldID"));
                fieldEvent.setFieldName(rs.getString("FieldName"));
                fieldEvent.setDescription(rs.getString("[Description]"));
                fieldEvent.setImages(rs.getString("Images"));

                lst.add(fieldEvent);
            }
        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return lst;
    }

    public Field_Event getFieldEventByID(int fieldID) {
        Field_Event fieldEvent = null;

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetFieldEventByID(?)}");
            call.setInt(1, fieldID);
            ResultSet rs = call.executeQuery();

            if (rs.next()) {
                fieldEvent = new Field_Event();
                fieldEvent.setFieldName(rs.getString("FieldName"));
                fieldEvent.setDescription(rs.getString("[Description]"));
                fieldEvent.setImages(rs.getString("Images"));
            } else {
                // Neu sai se thong bao loi
            }

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }

        return fieldEvent;
    }

    public Field_Event getFieldEventByName(String fieldName) {
        Field_Event fieldEvent = null;

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_GetFieldEventName(?)}");
            call.setString(1, fieldName);
            ResultSet rs = call.executeQuery();
            if (rs.next()) {
                fieldEvent.setFieldID(rs.getInt("FieldID"));
                fieldEvent.setDescription(rs.getString("[Description]"));
                fieldEvent.setImages(rs.getString("Images"));
            } else {
                // Neu sai thi thong bao loi
            }

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return fieldEvent;
    }

    public boolean insertFieldEvent(Field_Event fieldEvent) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_InsertFieldEvent(?,?,?)}");
            call.setString(1, fieldEvent.getFieldName());
            call.setString(2, fieldEvent.getDescription());
            call.setString(3, fieldEvent.getImages());

            call.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public boolean updateFieldEvent(Field_Event fieldEvent) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_UpdateFieldEvent(?,?,?,?)}");
            call.setInt(1, fieldEvent.getFieldID());
            call.setString(2, fieldEvent.getFieldName());
            call.setString(3, fieldEvent.getDescription());
            call.setString(4, fieldEvent.getImages());

            call.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public boolean deleteFieldEventByID(int fieldID) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_DeleteFieldEventByID(?)}");
            call.setInt(1, fieldID);
            call.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public boolean deleteFieldEventByName(String fieldName) {
        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_DeleteFieldEventByName(?)}");
            call.setString(1, fieldName);
            call.execute();

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return true;
    }

    public List<Field_Event> searchFieldEventByName(String fieldName) {
        List<Field_Event> lst = new ArrayList<>();

        try {
            CallableStatement call = con.getConnection().prepareCall("{call sp_SearchFieldEventByName(?)}");
            call.setString(1, fieldName);
            ResultSet rs = call.executeQuery();

            while (rs.next()) {
                Field_Event fieldEvent = new Field_Event();
                fieldEvent.setFieldID(rs.getInt("FieldID"));
                fieldEvent.setFieldName(rs.getString("FieldName"));
                fieldEvent.setDescription(rs.getString("[Description]"));
                fieldEvent.setImages(rs.getString("Images"));

                lst.add(fieldEvent);
            }

        } catch (SQLException ex) {
            Logger.getLogger(Field_EventDAL.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            con.closeDB();
        }
        return lst;
    }
}
